--
-- Author: ping.wang
-- Date: 2018-8-25 13:37:00
--
-- RewardThreeDayPanel
local ClassRef = app.mvc.createPanelClass(...)
local RewardLoginScrollList = import(".renders.RewardLoginScrollList")
local RewardDayLoginRender = import(".renders.RewardDayLoginRender")
--data =
function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardSignPanel))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef.create(node)
	return ClassRef.createWithData(nil, node)
end

function ClassRef:setSubType(subType)
	self.subType = subType
end

function ClassRef:init(data)
	self.super.init(self, data)
	self:setContentSize( self._ui.Panel:getContentSize() )

	self._tlt = self._ui["Panel/Panel_tlt/tlt"]

	self._signRenderNode = self._ui["Panel/page"]
	self._signRenderNode:findChild("text"):setString("")
    self._curSelectIdx = nil
	local receiveBtn = self._signRenderNode:findChild("btn_receive")
	receiveBtn:addClickEventListener(function()
		--准备领取奖励
        local data = self:_getDayAwardsInfoBySendIdx(self._curSelectIdx)
        if data then
			if self.subType and self.subType > 0 then
				executeFunc(self._onOperate, "getThreeDayAward", {idx = data.sendIdx,uuid = self._data.uuid})
			else
				executeFunc(self._onOperate, "getSignAward", {idx = data.idx,uuid = self._data.uuid})
			end
        end
    end)

	--设置一个模板遮罩
	local panelIconAddition = self._ui["Panel/Panel_tab"]
	local paramsAddition = {
		parentNode = panelIconAddition,
		size = panelIconAddition:getContentSize(),
		subNodeClass = RewardDayLoginRender,
		paddingLen = 0,
		selectedCb = handler(self,self.changeSelectedReward)
	}
	self._rewardLoginScrollList = RewardLoginScrollList.new( paramsAddition )

    self._initCanGetIdx = 1
	self:refresh()
end

function ClassRef:_getDayAwardsInfoBySendIdx(idx)
    if self._data then
        local dayAwards = self._data.dayAwards
        for k,v in ipairs(dayAwards) do
            if tonumber(v.sendIdx) == tonumber(idx) then
               return v
            end
        end
    end
    return nil
end


--通過索引獲知現在顯示第幾個
function ClassRef:_showAwardPanelByIndex(idx)
    local data = self:_getDayAwardsInfoBySendIdx(idx)
	if data then
		self._curSelectIdx = idx
		local dayIdx = data.sendIdx --第幾天的索引
		self._signRenderNode:findChild("Image"):loadTexture(Res.activityIconPath.."into_"..dayIdx..".png")
		self._signRenderNode:findChild("text"):setString(L(data.des))
		--[[
		for i = 1, 3 do
            local bl = i == dayIdx
			self._selectedBtns[i]:setBright(not bl)
            self._selectedBtns[i]:findChild("name_1"):setVisible(not bl)
            self._selectedBtns[i]:findChild("name_2"):setVisible(bl)
		end
		--]]
        local receiveBtn = self._signRenderNode:findChild("btn_receive")
		local receiveDot = self._signRenderNode:findChild("dot")

		--刷新的时候做
		if data.haveGet then
			receiveBtn:setEnabled(false)
			receiveBtn:findChild("name"):loadTexture(Res.activityIconPath.."btn_haveget.png")
			receiveBtn:setBright(false)
        else
			receiveBtn:setEnabled(data.canGet)
			receiveBtn:setBright(data.canGet)
			receiveBtn:findChild("name"):loadTexture(Res.activityIconPath.."btn_get.png")
		end

        receiveDot:setVisible(not data.haveGet and data.canGet)
	end
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:refresh()
	if self._data then
		self._tlt:setString(self._data.name)
		local dayAwards = self._data.dayAwards

		local scrollDataAddition = {}

		for k,v in ipairs(dayAwards) do
            local sendIdx = v.sendIdx

			if not self._curSelectIdx then
				self._curSelectIdx = sendIdx
			end
			if v.canGet and not v.haveGet then
                self._initCanGetIdx = k
			end

			scrollDataAddition[k] = {}
			scrollDataAddition[k].id = tonumber(k)
			scrollDataAddition[k].data = v
		end
		--第二个参数为第一个可以领的数据索引
		self._rewardLoginScrollList:setData(scrollDataAddition,self._initCanGetIdx)

		self:_showAwardPanelByIndex(self._curSelectIdx)

		self:setVisible( true )
	else
		self:setVisible( false )
	end
end

function ClassRef:setData(data)
	self._data = data
	self:refresh()
end

--选中的回调
function ClassRef:changeSelectedReward(data)
	self._curSelectIdx = data.data.sendIdx
	--self._baseIconCurIdx = data.id
	--self._guildIcon:setBaseIcon( data.baseIconId )
	local nodes = self._rewardLoginScrollList:getAllNodes()
	for k,v in ipairs(nodes) do
		v:setSelected(v:getIdx() == data.id)
	end
	--根据当前选中的数据刷新界面
	self:_showAwardPanelByIndex(self._curSelectIdx)
end

return ClassRef